package cn.rengy.web.excelsearch;

import cn.rengy.isql.Dao;
import cn.rengy.lang.ResultEntity;
import cn.rengy.tool.core.util._NumberUtils;
import cn.rengy.web.excelcompare.service.ExcelComporeService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
public class ExcelSearchController {

    @Autowired
    private ExcelSearchService excelSearchService;


    @Autowired
    private Dao dao;
    @RequestMapping("/excelsearch/task2")
    public ResultEntity task2() throws Exception {
        List<Map<String,Object>> list=dao.queryForList("select * from 调度线下电厂光伏");
        for(Map<String,Object> m:list){
            String 电厂名称核心=MapUtils.getString(m,"线下电厂名称核心");
            String 电厂名称=MapUtils.getString(m,"线下电厂名称");
            List<Map<String,Object>> mappingList=dao.queryForList("select * from 数据中台电厂光伏 where 线上电厂名称 like ?","%"+电厂名称核心+"%");

            if(mappingList!=null){
                List<String> idList=dao.querySingleColumnForList("select 线上电厂ID from 数据中台电厂光伏 where 线上电厂名称 like ?",String.class,"%"+电厂名称核心+"%");
                List<String> rlList=dao.querySingleColumnForList("select 线上电厂装机容量 from 数据中台电厂光伏 where 线上电厂名称 like ?",String.class,"%"+电厂名称核心+"%");
                dao.update("update 调度线下电厂光伏 set 线上电厂数量=?,线上电厂ID=?,线上电厂装机容量=? where 线下电厂名称=?",mappingList.size(),idList.toString(),rlList.toString(),电厂名称);
                for(Map<String,Object> map:mappingList){
                    String 电厂ID=MapUtils.getString(map,"线上电厂ID");
                    //BigDecimal db=dao.querySingleColumn("select sum(CAST(装机容量 AS DECIMAL)) s from 数据中台电厂 where 电厂名称 like ?", BigDecimal.class,"%"+电厂名称核心+"%");
                    dao.update("update 数据中台电厂光伏 set 线下电厂数量=线下电厂数量+1,keyw=CONCAT(IFNULL(keyw,''),',',?)  where 线上电厂ID=?",电厂名称核心,电厂ID);

                }
            }
        }
        return ResultEntity.ok();
    }

    @RequestMapping("/excelsearch/task1")
    public ResultEntity task1() throws Exception {
        List<Map<String,Object>> list=dao.queryForList("select * from 调度线下电厂");
        for(Map<String,Object> m:list){
            String 电厂名称核心=MapUtils.getString(m,"电厂名称核心");
            String 电厂名称=MapUtils.getString(m,"电厂名称");
            String 装机容量=MapUtils.getString(m,"装机容量");
            List<Map<String,Object>> mappingList=dao.queryForList("select * from 数据中台电厂 where 电厂名称 like ?","%"+电厂名称核心+"%");
            if(mappingList!=null){
                for(Map<String,Object> map:mappingList){
                    String 电厂ID=MapUtils.getString(map,"电厂ID");
                    //BigDecimal db=dao.querySingleColumn("select sum(CAST(装机容量 AS DECIMAL)) s from 数据中台电厂 where 电厂名称 like ?", BigDecimal.class,"%"+电厂名称核心+"%");
                    dao.update("update 数据中台电厂 set 线下电厂数量=?,keyw=?,线下电厂装机容量=? where 电厂ID=?",mappingList.size(),电厂名称核心,装机容量,电厂ID);
                    dao.update("update 调度线下电厂 set 线上电厂数量=? where 电厂名称=?",mappingList.size(),电厂名称);
                }
            }
        }
        return ResultEntity.ok();
    }

    /**
     * 导入子表
     * @param param
     * @return
     * @throws Exception
     */
    @RequestMapping("/excelsearch/importSubTable")
    public ResultEntity importSubTable(@RequestBody Map<String,Object> param) throws Exception {
        Long taskId=MapUtils.getLong(param,"taskId");
        excelSearchService.importSubTable(taskId);
        return ResultEntity.ok();
    }


    @RequestMapping("/excelsearch/initMapping")
    public ResultEntity initMapping(@RequestBody Map<String,Object> param) throws Exception {
        Long taskId=MapUtils.getLong(param,"taskId");
        excelSearchService.initMapping(taskId);
        return ResultEntity.ok();
    }

}
